package ihuotui.test.threadpool;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class NewFixedThreadPool {
    public static void main(String[] args) {
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
        ConcurrentHashMap<Integer, Object> syn = new ConcurrentHashMap<Integer, Object>();
        Integer m1 = 1;
        Integer m2 = 2;
        Integer m3 = 3;
        //使用公平锁，保证同个设备的线程顺序执行
        syn.put(m1, new ReentrantLock(true));
        //默认非公平锁，同个设备的线程乱序执行
        syn.put(m2, new ReentrantLock());
        //默认非公平锁，同个设备的线程乱序执行
        syn.put(m3, new Object());
        for (int i = 0; i < 60; i++) {
            if (i < 20) {
                SynPrint outPrint = new SynPrint((Object) syn.get(m3), i, m3);
                fixedThreadPool.execute(outPrint);
                continue;
            }
            if (i % 2 == 1) {
                LockPrint outPrint = new LockPrint((Lock) syn.get(m1), i, m1);
                fixedThreadPool.execute(outPrint);
            } else {
                LockPrint outPrint = new LockPrint((Lock) syn.get(m2), i, m2);
                fixedThreadPool.execute(outPrint);
            }

        }
    }
}


